package cn.com.homeyoda.aimate.aihealth.ui.detail.detail;

import android.app.Application;

import androidx.annotation.NonNull;

import com.wzq.mvvmsmart.event.StateLiveData;
import com.wzq.mvvmsmart.http.ResponseThrowable;
import com.wzq.mvvmsmart.utils.KLog;
import com.wzq.mvvmsmart.utils.RxUtils;
import com.wzq.mvvmsmart.utils.ToastUtils;

import cn.com.homeyoda.aimate.aihealth.base.BaseViewModel;
import cn.com.homeyoda.aimate.aihealth.bean.MessageListBean;
import cn.com.homeyoda.aimate.aihealth.bean.MessageTypeListBean;
import cn.com.homeyoda.aimate.aihealth.bean.PopularDetailBean;
import cn.com.homeyoda.aimate.aihealth.data.Repository;
import cn.com.homeyoda.aimate.aihealth.ui.message.MessageModel;
import io.reactivex.Observer;
import io.reactivex.disposables.Disposable;

public class DetailViewModel extends BaseViewModel<Repository> {
    public StateLiveData<PopularDetailBean.DataBean> stateLiveData;
    public String htmlContent = "<!DOCTYPE html>" +
            "<html lang=\"zh\">" +
            "<head>" +
            "  <meta charset=\"UTF-8\">" +
            "  <meta name=\"viewport\" content=\"width=device-width, initial-scale=1.0\">" +
            "  <title>教育文章</title>" +
            "  <style>" +
            "    body {" +
            "      background-color: transparent;" +
            "      margin: 0;" +
            "      font-family: \"Helvetica Neue\", Arial, sans-serif;" +
            "      color: #000;" +
            "      line-height: 1.7;" +
            "    }" +
            "    .content {" +
            "      max-width: 780px;" +
            "      margin: auto;" +
            "    }" +
            "    img {" +
            "      width: 100%;" +
            "      height: auto;" +
            "      margin: 16px 0;" +
            "    }" +
            "    p {" +
            "      margin-bottom: 1em;" +
            "      text-indent: 2em;" +
            "    }" +
            "  </style>" +
            "</head>" +
            "<body>" +
            "  <div class=\"content\">" +
            "    <p>教育永远是热门话题。两会中，未成年人“熬屏24小时”的提议挂上热搜，加强青少年科学健身普及和健康干预写入《政府工作报告》。</p>" +
            "    <p>开学季，各地连续出台加强学校体育的措施，让孩子们“身上有汗，眼里有光”成为热词。</p>" +
            "    <p>近期记者在云南红河州调研时看到，个旧市大屯中心校和下辖9所村完小里，最“顶配”的是体育老师——省人大代表洪家亮和省级名师正多势，将花式跳绳、啦啦操作为特色项目普及到全市学生。</p>" +
            "    <p>大山里的屏边县湾塘小学以“零近视”闻名，作为一所寄宿制学校，他们用篮球、排球、跳绳等18项文体活动填满了孩子们的课外时间。</p>" +
            "<div style=\"overflow: hidden; margin: 16px 0;\">" +
            "  <img src=\"file:///android_res/mipmap/ic_image.png\"" +
            "       style=\"float: left; width: 30%; margin-right: 16px; margin-bottom: 4px;\"" +
            "       alt=\"湾塘小学孩子们体育锻炼场景\">" +
            "  <p style=\"text-indent: 2em;\">红河州是边境州，屏边县还是当地最晚一批脱贫摘帽的贫困县。学校条件简陋，师资匮乏，操场是不标准的，体育是数学老师代教的。</p>" +
            "  <p style=\"text-indent: 2em;\">然而，早在教育部有明确要求之前，这里的孩子每天运动时间就超过两小时，老师们还想着设法发展武术、舞龙舞狮等特色活动。课间和课后，布大的校园里到处是孩子们生龙活虎的身影。</p>" +
            "</div>" +
            " <div style=\"font-size: 10px; color: gray;\">湾塘小学的孩子们正在进行体育锻炼。来源：新华社</div>" +
            "    <p>硬件这么差，体育活动却开展得这么好，最核心的是老师用心，校长关心，家长放心，孩子开心。洪家亮说，对于学校体育，文件写得再楚，关键是学校真抓实干，切实让政策落地。</p>" +
            "  </div>" +
            "</body>" +
            "</html>";

    public DetailViewModel(@NonNull Application application, Repository model) {
        super(application, model);
        stateLiveData = new StateLiveData<>();
    }

    public void getPopularDetail(String id){
        model.getPopularDetail(id)
                .compose(RxUtils.observableToMain())
                .compose(RxUtils.exceptionTransformer())
                .doOnSubscribe(DetailViewModel.this)
                .subscribe(new Observer<PopularDetailBean>() {
                    @Override
                    public void onSubscribe(Disposable d) {
                        stateLiveData.postLoading();
                    }

                    @Override
                    public void onNext(PopularDetailBean popularDetailBean) {
                        if(popularDetailBean.getCode() == 200){
//                            messageData.postSuccess();
//                            messageData.setValue(messageListBean);
                            stateLiveData.postValueAndSuccess(popularDetailBean.getData());
                        }
                    }

                    @Override
                    public void onError(Throwable e) {
                        KLog.e("进入onError" + e.getMessage());
                        //关闭对话框
                        stateLiveData.postError();
                        if (e instanceof ResponseThrowable) {
                            ToastUtils.showShort(((ResponseThrowable) e).message);
                        }
                    }

                    @Override
                    public void onComplete() {
                        KLog.e("进入onComplete");
                        //请求刷新完成收回
                        stateLiveData.postIdle();

                    }
                });
    }

}
